package com.solute.utils.image.impl;

import java.io.File;
import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;

import com.solute.utils.image.ImageRepository;

public class LocalImageRepository implements ImageRepository {
	
	private String repositoryPath;
	
	public LocalImageRepository(String repositoryPath) {
		this.repositoryPath = repositoryPath;
	}
	
	public String getPath() {
		return repositoryPath;
	}

	@Override
	public String store(String id, MultipartFile image) throws IOException {
		File f = new File(repositoryPath + '/' + id);
		if(!f.getParentFile().exists())
			f.getParentFile().mkdirs();
		
		image.transferTo(f);
		
		return f.getCanonicalPath();
	}

	@Override
	public void remove(String id) throws IOException {
		File f = new File(repositoryPath + '/' + id);
		if(f.exists())
			f.delete();
	}
	
	
}
